<?php

class Tev_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{

    /**
     * Called before an action is dispatched by Zend_Controller_Dispatcher.
     *
     * This callback allows for proxy or filter behavior.  By altering the
     * request and resetting its dispatched flag (via
     * {@link Zend_Controller_Request_Abstract::setDispatched() setDispatched(false)}),
     * the current action may be skipped.
     *
     * @param  Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $oAuth = Zend_Auth::getInstance();
        $oAcl = $this->getAcl();

        // Default role
        $role = 'all';
        if ($oAuth->hasIdentity()) {
            $role = $oAuth->getIdentity()->role;
        }

        Zend_View_Helper_Navigation_HelperAbstract::setDefaultAcl($oAcl);
        Zend_View_Helper_Navigation_HelperAbstract::setDefaultRole($role);

        $sModule     = $request->module;
        $sController = $request->controller;
        $sAction     = $request->action;
        $sResource   = $sModule . '/' . $sController . '/' . $sAction;

        if ($oAcl->has($sResource)) {
            if(!$oAcl->isAllowed('all', $sResource)) {
                // Access is not allowed
                if (!$oAcl->isAllowed($role, $sResource)) {
                    $flashMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
                    $flashMessenger->addMessage('Acesso negado');
                    $request->setModuleName('default');
                    $request->setControllerName('index');
                    $request->setActionName('index');
                }
            }
        } else {
            $flashMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
            $flashMessenger->addMessage('Recurso não encontrado : ' . $sResource);

            $flashMessenger = new Zend_Controller_Action_Helper_FlashMessenger();
            $flashMessenger->addMessage('Acesso negado');
            $request->setModuleName('default');
            $request->setControllerName('index');
            $request->setActionName('index');
        }
    }

    protected function getAcl() {
        $oAcl  = new Zend_Acl();

        // Perfis
        $oAcl->addRole(new Zend_Acl_Role('all'));
        $oAcl->addRole(new Zend_Acl_Role(User::ROLE_ENTERPRISE), array('all'));
        $oAcl->addRole(new Zend_Acl_Role(User::ROLE_ADMIN), array(User::ROLE_ENTERPRISE));

        // Controllers e Actions
        $oAcl->add(new Zend_Acl_Resource('default/index/index'));
        $oAcl->add(new Zend_Acl_Resource('default/index/login'));
        $oAcl->add(new Zend_Acl_Resource('default/index/logout'));
        $oAcl->add(new Zend_Acl_Resource('default/index/limpar-cache'));
        $oAcl->add(new Zend_Acl_Resource('default/index/limpar-cache-navigation'));

        $oAcl->add(new Zend_Acl_Resource('default/file/upload'));
        $oAcl->add(new Zend_Acl_Resource('default/file/check-by-task'));
        $oAcl->add(new Zend_Acl_Resource('default/file/list-by-task'));
        $oAcl->add(new Zend_Acl_Resource('default/file/get'));

        $oAcl->add(new Zend_Acl_Resource('default/contact/index'));
        $oAcl->add(new Zend_Acl_Resource('default/contact/send'));

        $oAcl->add(new Zend_Acl_Resource('task/dashboard/index'));
        $oAcl->add(new Zend_Acl_Resource('task/dashboard/admin'));

        $oAcl->add(new Zend_Acl_Resource('task/project/index'));
        $oAcl->add(new Zend_Acl_Resource('task/project/list'));
        $oAcl->add(new Zend_Acl_Resource('task/project/form'));
        $oAcl->add(new Zend_Acl_Resource('task/project/save'));

        $oAcl->add(new Zend_Acl_Resource('task/task/index'));
        $oAcl->add(new Zend_Acl_Resource('task/task/list'));
        $oAcl->add(new Zend_Acl_Resource('task/task/form'));
        $oAcl->add(new Zend_Acl_Resource('task/task/save'));
        $oAcl->add(new Zend_Acl_Resource('task/task/see'));
        $oAcl->add(new Zend_Acl_Resource('task/task/close'));
        $oAcl->add(new Zend_Acl_Resource('task/task/open'));
        $oAcl->add(new Zend_Acl_Resource('task/task/comment'));
        $oAcl->add(new Zend_Acl_Resource('task/task/forward'));
        $oAcl->add(new Zend_Acl_Resource('task/task/inactivate'));

        $oAcl->add(new Zend_Acl_Resource('task/type-task/list'));
        $oAcl->add(new Zend_Acl_Resource('task/type-task/form'));
        $oAcl->add(new Zend_Acl_Resource('task/type-task/save'));

        $oAcl->add(new Zend_Acl_Resource('task/report/index'));
        $oAcl->add(new Zend_Acl_Resource('task/report/by-projects'));

        $oAcl->add(new Zend_Acl_Resource('enterprise/enterprise/index'));
        $oAcl->add(new Zend_Acl_Resource('enterprise/enterprise/list'));
        $oAcl->add(new Zend_Acl_Resource('enterprise/enterprise/form'));
        $oAcl->add(new Zend_Acl_Resource('enterprise/enterprise/save'));

        $oAcl->add(new Zend_Acl_Resource('enterprise/user/index'));
        $oAcl->add(new Zend_Acl_Resource('enterprise/user/list'));
        $oAcl->add(new Zend_Acl_Resource('enterprise/user/form'));
        $oAcl->add(new Zend_Acl_Resource('enterprise/user/save'));

        $oAcl->add(new Zend_Acl_Resource('task/cron/index'));

        // Permissões
        $oAcl->allow('all', 'default/index/index');
        $oAcl->allow('all', 'default/index/login');
        $oAcl->allow('all', 'default/index/logout');
        $oAcl->allow('all', 'default/index/limpar-cache');
        $oAcl->allow('all', 'task/cron/index');
        $oAcl->allow('all', 'default/file/upload');
        $oAcl->allow(User::ROLE_ADMIN, 'default/index/limpar-cache-navigation');

        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/dashboard/index');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/see');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/close');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/open');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/comment');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/forward');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/form');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/save');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/task/inactivate');

        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/report/index');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'task/report/by-projects');

        $oAcl->allow(User::ROLE_ENTERPRISE, 'default/file/upload');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'default/file/check-by-task');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'default/file/list-by-task');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'default/file/get');

        $oAcl->allow(User::ROLE_ENTERPRISE, 'default/contact/index');
        $oAcl->allow(User::ROLE_ENTERPRISE, 'default/contact/send');

        $oAcl->allow(User::ROLE_ADMIN, 'task/dashboard/admin');

        $oAcl->allow(User::ROLE_ADMIN, 'task/project/index');
        $oAcl->allow(User::ROLE_ADMIN, 'task/project/list');
        $oAcl->allow(User::ROLE_ADMIN, 'task/project/form');
        $oAcl->allow(User::ROLE_ADMIN, 'task/project/save');

        $oAcl->allow(User::ROLE_ADMIN, 'task/task/index');
        $oAcl->allow(User::ROLE_ADMIN, 'task/task/list');

        $oAcl->allow(User::ROLE_ADMIN, 'task/type-task/list');
        $oAcl->allow(User::ROLE_ADMIN, 'task/type-task/form');
        $oAcl->allow(User::ROLE_ADMIN, 'task/type-task/save');

        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/enterprise/index');
        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/enterprise/list');
        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/enterprise/form');
        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/enterprise/save');

        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/user/list');
        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/user/form');
        $oAcl->allow(User::ROLE_ADMIN, 'enterprise/user/save');

        return $oAcl;
    }
}
